/**
 * 
 */
package com.rohan.apps.mileagetracker.client.ui;

import java.util.List;
import com.google.gwt.core.client.GWT; 
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.ListBox;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.rohan.apps.mileagetracker.shared.proxies.AppUserProxy;
import com.rohan.apps.mileagetracker.shared.proxies.VehicleProxy;
import com.rohan.apps.mileagetracker.shared.service.AppUserRequest;
import com.rohan.apps.mileagetracker.shared.service.MileageTrackerRequestFactory;
import com.rohan.apps.mileagetracker.shared.service.VehicleRequest;
import com.google.gwt.user.client.ui.SimplePanel;

/**
 * @author R!
 *
 */
public class WiringTest extends Composite  {

	private static WiringTestUiBinder uiBinder = GWT
			.create(WiringTestUiBinder.class);
	MileageTrackerRequestFactory requestFactory;
	final DialogBox dialogBox = new DialogBox();
	interface WiringTestUiBinder extends UiBinder<Widget, WiringTest> {
	}

	/**
	 * Because this class has a default constructor, it can
	 * be used as a binder template. In other words, it can be used in other
	 * *.ui.xml files as follows:
	 * <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
	 *   xmlns:g="urn:import:**user's package**">
	 *  <g:**UserClassName**>Hello!</g:**UserClassName>
	 * </ui:UiBinder>
	 * Note that depending on the widget that is used, it may be necessary to
	 * implement HasHTML instead of HasText.
	 */
	public WiringTest() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	public WiringTest(MileageTrackerRequestFactory requestFactory){
		this.requestFactory=requestFactory;
		initWidget(uiBinder.createAndBindUi(this));
		addLoginAnchor();
	}

	@UiField
	Button button;
	@UiField ListBox lstVehicles;
	
	@UiField SimplePanel pnlLogin;
	
	Anchor anLogIn= new Anchor();

	public WiringTest(String firstName) {
		initWidget(uiBinder.createAndBindUi(this));

		// Can access @UiField after calling createAndBindUi
		button.setText(firstName);
	}

	@UiHandler("button")
	void onClick(ClickEvent e) {
		showMessage("Creating Vehicle list...");
		VehicleRequest request = requestFactory.vehicleRequest();
		
		
	
		/*final VehicleProxy audiA4= request.create(VehicleProxy.class);
		audiA4.setMake("Audi");
		audiA4.setModel("A4");
		audiA4.setTrim("Premium Plus");
		audiA4.setYear("2011");
		audiA4.setName("Knight-Rider");
		
		final VehicleProxy fordFusion= request.create(VehicleProxy.class);
		fordFusion.setMake("Ford");
		fordFusion.setModel("Fusion");
		fordFusion.setTrim("XL");
		fordFusion.setYear("2011");
		fordFusion.setName("Deep's Car");*/
	
		request.listAllvehicles().fire(new Receiver<List<VehicleProxy>>(){

			@Override
			public void onSuccess(List<VehicleProxy> response) {
				// TODO Auto-generated method stub
				hideMessage();
				//build list
				for(VehicleProxy vehicle : response){
					lstVehicles.addItem(vehicle.getName()+" "+vehicle.getMake()+"-"+vehicle.getModel(),vehicle.getId().toString());
				}
				
				
			}
			
		});	
		
		/*request.saveVehicle(audiA4).fire(new Receiver<Void>(){

			@Override
			public void onSuccess(Void response) {
				// TODO Auto-generated method stub
				hideMessage();
				showMessage("Audi A4 saved");
				VehicleRequest request = requestFactory.vehicleRequest();
				request.saveVehicle(fordFusion).fire(new Receiver<Void>(){

					@Override
					public void onSuccess(Void response) {
						// TODO Auto-generated method stub
						hideMessage();
						showMessage("Ford fusion saved");
						VehicleRequest request = requestFactory.vehicleRequest();
						request.listAllvehicles().fire(new Receiver<List<VehicleProxy>>(){

							@Override
							public void onSuccess(List<VehicleProxy> response) {
								// TODO Auto-generated method stub
								hideMessage();
								//build list
								for(VehicleProxy vehicle : response){
									lstVehicles.addItem(vehicle.getName()+" "+vehicle.getMake()+"-"+vehicle.getModel(),vehicle.getId().toString());
								}
								
								
							}
							
						});
						
					}
					
				});
			}
			
		});*/

		
	
	}

	private void showMessage(String htmlStr){
		
		dialogBox.setText("Request Factory Call");
		dialogBox.setAnimationEnabled(true);		
		dialogBox.center();
		VerticalPanel dialogVPanel = new VerticalPanel();
		final Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		// Add a handler to close the DialogBox
				closeButton.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						dialogBox.hide();						
					}
				});
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML(htmlStr));		
			
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
		dialogBox.show();
	}
	
	private void hideMessage(){
		dialogBox.hide();		
	}
	
	private void addLoginAnchor(){
		AppUserRequest userRequest = requestFactory.appUserRequest();
		userRequest.LogIn(GWT.getHostPageBaseURL()).fire(new Receiver<AppUserProxy>(){

			@Override
			public void onSuccess(AppUserProxy response) {
				// TODO Auto-generated method stub
				if(!response.isLoggedIn()){
					Window.Location.assign(response.getLoginUrl());
					anLogIn.setHref((response.getLoginUrl()));
					anLogIn.setTarget("_blank");
					anLogIn.setText("Log in");
				}
				else{
					anLogIn.setHref((response.getLogoutUrl()));
					anLogIn.setText("Log Out");
					anLogIn.setTarget("_blank");
				}
					
			}
			
			
		});
		pnlLogin.add(anLogIn);
	}
}
